const {
	Service
} = require('uni-cloud-router')

module.exports = class HelloService extends Service {
	constructor(arg) {
		super(...arguments)
	}
	async get_project(data) {
		const {
			db,
			$userinfo
		} = this.ctx
		const uid = $userinfo._id
		const dbCmd = db.command
		const $ = db.command.aggregate
		// let project = await db.collection('db-project-list').where({
		// 	user_id: uid
		// }).get()
		let project = db.collection('db-project-list').aggregate()
		project = await project
			.match({
				user_id: uid
			})
			.lookup({
				from: 'db-project-api',
				let: {
					id: '$_id'
				},
				pipeline: $.pipeline()
					.match(dbCmd.expr($.eq(['$project_id', '$$id'])))
					.done(),
				as: 'apis',
			})
			.end()
		//返回数据给客户端
		return {
			code: 0,
			lists: project.data
		}
	}

	async create_project() {
		const {
			$userinfo,
			db,
			event
		} = this.ctx
		const uid = $userinfo._id || ''
		const params = event.data
		// 判断参数是否存在
		if (!params.name || !params.path) {
			return {
				code: 2,
				desc: '参数错误'
			}
		}
		// 查询每个账户下是否存在相同的项目名称或路径
		let project = db.collection('db-project-list').aggregate()
		project = await project.match({
				user_id: uid
			})
			.match(db.command.or({
				project_name: params.name
			}, {
				project_base_path: params.path
			}))
			.end()
		// 验重
		if (project.data.length > 0) {
			let pdata = project.data
			let msg = ''
			for (let i = 0; i < pdata.length; i++) {
				// 只验证同一个用户
				if (params.name === pdata[i].project_name) {
					msg = '项目名称已经存在'
					break
				}
				if (params.path === pdata[i].project_base_path) {
					msg = '基础路径已经存在'
					break
				}
			}

			return {
				code: 2,
				desc: msg
			}
		}

		let addObj = {
			"user_id": uid,
			"project_name": params.name,
			"project_base_path": params.path,
			"comment": params.comment || '',
			"created_date": Date.now(),
			"status": 1,
			"apis": []
		}
		// 添加数据
		let projectCallback = await db.collection('db-project-list').add(addObj);

		if (!projectCallback.id) {
			return {
				code: 2,
				desc: '新建项目失败'
			}
		}
		addObj._id = projectCallback.id
		//event为客户端上传的参数
		console.log(projectCallback);
		//返回数据给客户端
		return {
			code: 0,
			desc: '项目创建成功',
			data: addObj
		}
	}

	async create_api() {
		const {
			$userinfo,
			db,
			event
		} = this.ctx
		const uid = $userinfo._id || ''
		const params = event.data
		console.log(params);
		if (!params.project_id) {
			return {
				code: 2,
				desc: 'project_id 参数无效 ，请检查代码'
			}
		}

		// 判断参数是否存在
		if (!params.name || !params.path) {
			return {
				code: 2,
				desc: '参数错误'
			}
		}
		// 查询每个账户下是否存在相同的项目名称或路径
		let project = db.collection('db-project-api').aggregate()
		project = await project.match({
				user_id: uid,
				project_id: params.project_id
			})
			.match(db.command.or({
				api_base_name: params.name
			}, {
				api_base_path: params.path
			}))
			.end()
		// 验重
		if (project.data.length > 0) {
			let pdata = project.data
			let msg = ''
			for (let i = 0; i < pdata.length; i++) {
				// 只验证同一个用户
				if (params.name === pdata[i].api_base_name) {
					msg = 'API名称已经存在'
					break
				}
				if (params.path === pdata[i].api_base_path) {
					msg = 'API基础路径已经存在'
					break
				}
			}

			return {
				code: 2,
				desc: msg
			}
		}

		let addObj = {
			"user_id": uid,
			"project_id": params.project_id,
			"api_name": params.name,
			"api_base_path": params.path,
			"api_comment": params.comment || '',
			"created_date": Date.now(),
			"status": 1
		}
		// 添加数据
		let projectCallback = await db.collection('db-project-api').add(addObj);

		if (!projectCallback.id) {
			return {
				code: 2,
				desc: '新建API失败'
			}
		}
		addObj._id = projectCallback.id
		//event为客户端上传的参数
		console.log(projectCallback);
		//返回数据给客户端
		return {
			code: 0,
			desc: 'API创建成功',
			data: addObj
		}
	}
	async get_api_detail() {
		const {
			$userinfo,
			db,
			event
		} = this.ctx
		const uid = $userinfo._id || ''
		const params = event.data
		let project = await db.collection('db-project-api').where({
			_id: params.id,
			project_id: params.project_id,
			user_id: uid
		}).get()
		if (!project) return {
			code: 2,
			desc: '获取失败'
		}
		return {
			code: 0,
			desc: 'api获取成功',
			data: project.data[0]
		}
	}
	async save_api() {
		const {
			$userinfo,
			db,
			event
		} = this.ctx
		const uid = $userinfo._id || ''
		const params = event.data
		let project = await db.collection('db-project-api').where({
			_id: params.id,
			project_id: params.project_id,
			user_id: uid
		}).update({
			detail: params.detail
		})
		console.log(project);
		if (project.requestId) {
			return {
				code: 0,
				desc: 'API 更新成功'
			}
		}
		return {
			code: 2,
			desc: '更新失败'
		}
	}
}
